// 子模块moduleA路径：store/modules/moduleA.js
import createPersistedState from 'vuex-persistedstate';
const userApi = uniCloud.importObject('user')
const db = uniCloud.database();
export default {
	state: {
		userinfo: uni.getStorageSync('userinfo')
	},
	mutations: {
		setUserInfo(state, userinfo) {
			state.userinfo = userinfo;
			uni.setStorageSync('userinfo',userinfo)
		},
		clearUserInfo(state) {
			state.userinfo = null;
		},
		//自己新增的 
		setUserAvatar(state, avatr_file) {
			state.userinfo.avatar_file = avatr_file;
			console.log(state.userinfo)
		}
	},
	actions: {
		async fetchUser({
			commit
		}, token) {
			try {
				const result = await userApi.getUserId(token);
				const uid = result.data.uid;
				const userRes = await userApi.getUserInfoById(uid);
				if (userRes.data && userRes.code == 200) {
					commit('setUserInfo', userRes.data);
				}
			} catch (error) {
				console.error('获取用户信息失败:', error);
			}
		},
		logout({
			commit
		}) {
			commit('clearUserInfo');
		}
	},
	getters: {
		isAuthenticated(state) {
			return !!state.userinfo;
		},
		userinfo(state) {
			return state.userinfo;
		}
	}
}